/**
* 从Js读取图片位置信息
*/

package com.tudou.top2.controller
{
	import com.tudou.top2.events.PictEvent;
	import com.tudou.top2.model.AdUrlModel;
	import com.tudou.top2.model.PictUrlModel;
	
	import flash.events.EventDispatcher;
	import flash.events.TimerEvent;
	import flash.external.ExternalInterface;
	import flash.utils.Timer;
	
	public class ReadFromJs extends EventDispatcher
	{
		//视频图片Url
		private var pictsUrlArray:Array;
		//广告图片Url
		private var adUrlModel:AdUrlModel;
		
		private var callJsTimer:Timer; 
		
		public function ReadFromJs()
		{
			callJsTimer = new Timer(100);
			
			callJsTimer.addEventListener(TimerEvent.TIMER, callJs);
			
			pictsUrlArray = new Array();
			adUrlModel = new AdUrlModel();
		}
		
		//开始从JS读取数据
		public function read():void
		{
			callJsTimer.start();
		}
		
		//调用Js方法
		private function callJs(te:TimerEvent):void
		{
			if(ExternalInterface.available)
			{
				var rawArray:Array = ExternalInterface.call(KiliTop2.getSmallBigPictFunName) as Array;
				var advs:Array = ExternalInterface.call(KiliTop2.getAdInfoFunName) as Array;
				
				if(rawArray != null && rawArray.length != 0 && advs != null)
				{
					callJsTimer.stop();
					processArray(rawArray);
					processAd(advs);
					
					//发送完成
					dispatchEvent(new PictEvent(PictEvent.READ_URL_FINISHED));
				}
				else
				{
					trace("empty");
				}
			}
		}

		private function isPosition(item:*, index:int, array:Array):Boolean
		{
			return item.adPositionId == KiliTop2.monitor.adPositionId;
		}
		
		private function processAd(advs:Array):void
		{
			if(advs == null) return ;
			
			advs = advs.filter(isPosition);
			//没有广告
			if(advs == null || advs.length == 0)
			{
				adUrlModel.hasAd = false;
				return ;
			}
			else
			{
				for(var i:int=0; i<advs.length; i++)
				{
					switch(advs[i].positionOrderNum)
					{
						case 8:
							adUrlModel.adIconUrl = advs[i].imgUrl;
							break;
						case 9:
							adUrlModel.adPictUrl = advs[i].imgUrl;
							adUrlModel.adLink = advs[i].link;
							KiliTop2.monitor.isBar = "0";
							KiliTop2.monitor.throwId = advs[i].id;
							KiliTop2.monitor.ownerId = advs[i].ownerId;
							KiliTop2.monitor.link = advs[i].link;
							KiliTop2.monitor.isNielson = advs[i].isNielson;
							break;
						default:
							trace("Bad Postion " + advs[i].positionOrderNum);	
					}
				}
				if(adUrlModel.adIconUrl != null && adUrlModel.adPictUrl != null)
				{
					adUrlModel.hasAd = true;
				}
				else
				{
					adUrlModel.hasAd = false;
				}
			}
			trace("adIconUrl = " + adUrlModel.adIconUrl);
			trace("adPictUrl = " + adUrlModel.adPictUrl);
			trace("adLink = " + adUrlModel.adLink);
		}
		
		//处理广告
		private function processArray(raw:Array):void
		{
			if(raw.length < 5)
			{
				throw new Error("the url is less than 5 !");
			}
			//最多处理10个
			for(var i:uint=0; i<raw.length && i<10; i++)
			{
				var model:PictUrlModel = new PictUrlModel();
				var obj:Object = raw[i];
				if(obj.textColor != undefined && obj.textColor != null && obj.textColor != "")
				{
					model.textColor = obj.textColor;
				}
				else
				{
					model.textColor = "#FFFFFF";
				}
				model.title = obj.title;
				if(model.title == null)
				{
					model.title = "";
				}
				model.smallPicUrl = obj.smallPicUrl;
				model.link = obj.link;
				if(model.link == null) model.link = "";
				model.bigPicUrl = obj.bigPicUrl;
				model.bigTitle = obj.subTitle;
				//做非法处理
				if(model.bigTitle == null)
				{
					model.bigTitle = "";
				}
				model.bigTitle = model.bigTitle.replace("\r\n", "\n");
				model.bigTitle = model.bigTitle.replace("\n\r", "\n");
				if(obj.bigTextColor != undefined)
				{
					model.bigTextColor = obj.bigTextColor;
				}
				else
				{
					model.bigTextColor = "#FFFFFF";
				}
				
				pictsUrlArray.push(model);
			}
			
		}
		
		private function print(a:Array):void
		{
			for each(var model:Object in a)
			{
				trace(model.textColor);
				trace(model.title);
				trace(model.link);
				trace(model.smallPicUrl);
				trace(model.bigPicUrl);
			}
		}
		
		public function get pictsUrl():Array
		{
			return pictsUrlArray;
		}
		
		public function get adUrl():AdUrlModel
		{
			return adUrlModel;
		}
		
	}
}